package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.SHomepageImg;
import com.borrowed.book.book.domain.SelectHomeImgInDto;
import com.borrowed.book.book.domain.UpdHomeImgStatusInDto;
import com.borrowed.book.book.service.HomepageImgService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "首页图片管理")
@RequestMapping("/api/homepageImg")
public class HomepageImgController extends BaseController {
    @Autowired
    private HomepageImgService homepageImgService;


    @ApiOperation(value = "添加",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addHomepageImg(@RequestBody SHomepageImg param){
        AjaxResult result;
        try{
            homepageImgService.AddHomepageImg(param);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "修改",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResult updateHomepageImg(@RequestBody SHomepageImg param){
        AjaxResult result;
        try{
            homepageImgService.UpdateHomepageImg(param);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改状态（启用禁用）",httpMethod = "PUT")
    @PutMapping("/updateStatus")
    @ResponseBody
    @Transactional
    public AjaxResult updateHomepageImg(@RequestBody UpdHomeImgStatusInDto param){
        AjaxResult result;
        try{
            homepageImgService.UpdateHomepageImgStatus(param);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除",httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name="id",value = "id",type = "Long",required = true)
    @ResponseBody
    public AjaxResult deleteHomepageImg(Long id){
        AjaxResult result;
        try{
            homepageImgService.deleteHomepageImg(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "一览查询",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo selectHomepageList(SelectHomeImgInDto param){
        startPage();
        List<SHomepageImg> list=homepageImgService.selectHomepageImg(param);
        return getDataTable(list);
    }

}
